package com.etms.service;

import com.etms.mapper.CustomerMapper;
import com.etms.pojo.Customer;
import com.etms.pojo.Supplier;
import com.etms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerService {
    @Autowired
    CustomerMapper customerMapper;

    public Customer findById(Integer keId) {
        return customerMapper.findById(keId);
    }

    public Result findAll() {
        List<Customer> all = customerMapper.findAll();
        return Result.successed(all);
    }

    public Result findSuById(Integer id) {
        Customer byId = findById(id);
        return Result.successed(byId);
    }

    public Result delSuById(Integer id) {
        int cnt=customerMapper.delById(id);
        if(cnt==0)return Result.failed("删除失败");
        return Result.successed("删除成功",null);
    }

    public Result updateOrAdd(Customer customer) {
        if (customer.getId()==0||customer.getId()==null){//新增
            int cnt=customerMapper.addOne(customer);
            if(cnt==0)return Result.failed("新增失败");

            return Result.successed("新增成功!",null);
        }
        //否则是更新
        int cnt=customerMapper.updateOne(customer);
        if (cnt==0)return Result.failed("更新失败");
        return Result.successed("更新成功",null);
    }
}
